package ctd.util.converter;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.ISODateTimeFormat;

import java.util.Date;

/**
 * Created by jinxin on 2016/7/10.
 */
public class DateConverter {

    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATETIME1_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DATETIME2_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
    private static final String DATETIME3_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    private static final String DATETIME4_FORMAT = "yyyy-MM-dd HH:mm";

    public static String DateTimeToString(Date source) {
        return DateTimeFormat.forPattern(DATETIME1_FORMAT).print(source.getTime());
    }

    public static String DateToString(Date source){
        return DateTimeFormat.forPattern(DATE_FORMAT).print(source.getTime());
    }

    public static String DateToString(Date source,String format){
        return DateTimeFormat.forPattern(format).print(source.getTime());
    }

    public static Date StringToDateTime(String source){
        if(StringUtils.isEmpty(source)){
            return null;
        }
        int len = source.length();
        if(StringUtils.contains(source, "T")){
            if(StringUtils.contains(source, "Z")){
                if(len == 20){
                    return DateTimeFormat.forPattern(DATETIME3_FORMAT).withZoneUTC().parseDateTime(source).toDate();
                }
                else{
                    return ISODateTimeFormat.dateTime().parseDateTime(source).toDate();
                }
            }
            return DateTimeFormat.forPattern(DATETIME2_FORMAT).withZoneUTC().parseDateTime(source).toDate();
        }
        else if(StringUtils.contains(source, ":")){
            if(len == 16){
                return DateTimeFormat.forPattern(DATETIME4_FORMAT).parseDateTime(source).toDate();
            }
            return DateTimeFormat.forPattern(DATETIME1_FORMAT).parseDateTime(source).toDate();
        }
        else  if(StringUtils.contains(source, "-")){
            return DateTimeFormat.forPattern(DATE_FORMAT).parseLocalDate(source).toDate();
        }
        else if(StringUtils.equals(source.toLowerCase(), "now")){
            return new Date();
        }
        else if(StringUtils.equals(source.toLowerCase(), "today")){
            return (new DateTime()).withTimeAtStartOfDay().toDate();
        }
        else if(StringUtils.equals(source.toLowerCase(), "yesterday")){
            return (new LocalDate().minusDays(1).toDate());
        }
        else if(StringUtils.equals(source.toLowerCase(), "tomorrow")){
            return (new LocalDate().plusDays(1).toDate());
        }
        else{
            throw new IllegalArgumentException("Invalid date string value '" + source + "'");
        }
    }

    public static Date StringToDate(String source){
        return DateTimeFormat.forPattern(DATE_FORMAT).parseDateTime(source).toDate();
    }

    public static Date StringToDate(String source,String format){
        return DateTimeFormat.forPattern(format).parseDateTime(source).toDate();
    }
}
